package com.dms.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dms.common.core.domain.Ztree;
import com.dms.system.domain.SysPlant;
import com.dms.system.domain.dto.SysAreaDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dms.system.mapper.SysAreaMapper;
import com.dms.system.domain.SysArea;
import com.dms.system.service.ISysAreaService;
import com.dms.common.core.text.Convert;

/**
 * 设备所属区域Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService 
{
    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Override
    public List<SysAreaDTO> selectAreaPlantsByMissionId(Long missionId) {
        return sysAreaMapper.selectSysAreaDTOListByMissionId(missionId);
    }

    @Override
    public List<Ztree> selectAreaTree(SysArea sysArea) {
        List<SysAreaDTO> sysAreaDTOList = sysAreaMapper.selectSysAreaDTOList(sysArea);
        List<Ztree> ztreeList = initZtree(sysAreaDTOList);
        return ztreeList;
    }

    /**
     * 对象转区域设备树
     * @param sysAreaDTOList 区域设备DTO对象列表
     * @return 树结构列表
     * @author xlong_zhu
     * @date 2021/4/9
     */
    public List<Ztree> initZtree(List<SysAreaDTO> sysAreaDTOList){
        List<Ztree> ztreeList = new ArrayList<>();
        Long pid;
        List<SysPlant> plantList;
        Ztree ztree;
        for(SysAreaDTO sysAreaDTO:sysAreaDTOList){
            // 设置区域
            ztree = new Ztree();
            ztree.setId(sysAreaDTO.getAreaId());
            ztree.setName(sysAreaDTO.getAreaName());
            ztree.setTitle(sysAreaDTO.getAreaName());
            pid = sysAreaDTO.getAreaId();
            plantList = sysAreaDTO.getPlantList();
            ztreeList.add(ztree);
            // 设置区域下的设备
            for(SysPlant sysPlant : plantList){
                ztree = new Ztree();
                ztree.setId(Long.parseLong(pid+""+sysPlant.getId()));
                ztree.setpId(pid);
                ztree.setName(sysPlant.getPlantName());
                ztree.setTitle(sysPlant.getPlantName());
                ztreeList.add(ztree);
            }
        }
        return ztreeList;
    }

    /**
     * 查询设备所属区域
     * 
     * @param id 设备所属区域ID
     * @return 设备所属区域
     */
    @Override
    public SysArea selectSysAreaById(Long id)
    {
        return sysAreaMapper.selectSysAreaById(id);
    }

    /**
     * 查询设备所属区域列表
     * 
     * @param sysArea 设备所属区域
     * @return 设备所属区域
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea)
    {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增设备所属区域
     * 
     * @param sysArea 设备所属区域
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea)
    {
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 修改设备所属区域
     * 
     * @param sysArea 设备所属区域
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea)
    {
        return sysAreaMapper.updateSysArea(sysArea);
    }

    /**
     * 删除设备所属区域对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(String ids)
    {
        return sysAreaMapper.deleteSysAreaByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备所属区域信息
     * 
     * @param id 设备所属区域ID
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long id)
    {
        return sysAreaMapper.deleteSysAreaById(id);
    }
}
